package com.lancoo.znbkxx.teacher.ui.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.webkit.WebChromeClient
import android.webkit.WebViewClient
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.adapter.QuestionAdapter
import com.lancoo.znbkxx.teacher.base.BaseVMFragment
import com.lancoo.znbkxx.teacher.databinding.FmtCommentClozeSelectionBinding
import com.lancoo.znbkxx.teacher.model.data.Child
import com.lancoo.znbkxx.teacher.utils.HtmlUtils
//import com.lancoo.znbkxx.teacher.model.data.OptionX
//import com.lancoo.znbkxx.teacher.model.data.SubChildX
import com.lancoo.znbkxx.teacher.utils.WebStyleUtils
import com.lancoo.znbkxx.teacher.viewmodel.HardSolutionViewModel

/**
 * @author : Admin
 * @date   : 2019/8/7 11:17
 * @desc   :
 */
class SingleChoiceFragment :
    BaseVMFragment<HardSolutionViewModel, FmtCommentClozeSelectionBinding>() {
    lateinit var adapter: QuestionAdapter
    private val foot by lazy { layoutInflater.inflate(R.layout.item_choice_footer, null) }
    val data = mutableListOf<Child>()
    val type by lazy { requireArguments().getString("title") }
    val genre by lazy { viewModel.hardSolution.value!!.TkModel.GenreID }


    companion object {
        private const val TITLE = "title"
        fun newInstance(title: String): SingleChoiceFragment {
            val fragment = SingleChoiceFragment()
            val bundle = Bundle()
            bundle.putString(TITLE, title)
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun providerVMClass(): Class<HardSolutionViewModel>? {
        return HardSolutionViewModel::class.java
    }

    override fun providerContext(): Any? {
        return activity
    }

    override fun setLayoutID(): Int {
        return R.layout.fmt_comment_cloze_selection
    }

    @SuppressLint("SetTextI18n")
    override fun initView() {
        binding.tvQuestionname.text =
            if (viewModel.hardSolution.value!!.TkModel.GenreName.isBlank()) viewModel.hardSolution.value!!.TkModel.TypeName else viewModel.hardSolution.value!!.TkModel.GenreName
        binding.tvQuestionnum.text =
            "(共${viewModel.hardSolution.value!!.TkModel.TotalItemCount}个小题)"
        binding.tvFrom.text= HtmlUtils.setText("来源:<font color='#0099ff'>${viewModel.hardSolution.value!!.TkModel.Resource}</font>")
        initWebView()
        initRecyclerView()
    }

    @SuppressLint("SetJavaScriptEnabled")
    private fun initWebView() {
        binding.wv.settings.javaScriptEnabled = true
        binding.wv.webViewClient = WebViewClient()
        binding.wv.webChromeClient = WebChromeClient()
        val body = viewModel.hardSolution.value!!.TkModel.ChildList[0].ChildAsk
        WebStyleUtils.showWebViewCssStyle(activity, binding.wv, body, false)
    }

    private fun initRecyclerView() {
        binding.rv.layoutManager =
            LinearLayoutManager(requireContext()).apply { orientation = RecyclerView.VERTICAL }
        adapter = QuestionAdapter(
            viewModel.hardSolution.value!!.StuAllCount,
            type,
            genre.toInt(),
            R.layout.item_question,
            data,
            lifecycle,
            null
        )
        adapter.addFooterView(foot)
        binding.rv.adapter = adapter
    }

    override fun onStart() {
        super.onStart()
        data.clear()
        val sb = StringBuffer()
        val sbAnlysis = StringBuffer()
        val childxList = viewModel.hardSolution.value!!.TkModel.ChildList
        data.addAll(childxList)
        if (childxList.size > 1) {
            childxList.forEachIndexed { i, childx ->
                if (childx.ChildAnalysis.isEmpty()) {
                    sbAnlysis.append("${i + 1}. ...略\n")
                } else {
                    sbAnlysis.append("${i + 1}.${childx.ItemList[0].ItemAnalysis}\n")
                }
                sb.append("${i + 1}.${childx.ItemList[0].ItemAnswer}  ")
            }
        } else {
            sbAnlysis.append("...略")
            sb.append(childxList[0].ItemList[0].ItemAnswer)
        }
        foot.findViewById<TextView>(R.id.tv_answer).text = sb
        if (sbAnlysis.contains("略")) {
            foot.findViewById<TextView>(R.id.tv_analyze).text = "...略"
        } else {
            foot.findViewById<TextView>(R.id.tv_analyze).text = sbAnlysis
        }
    }
}